<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 找出数组中第K大和第m大的数字相加之和(只能满足相同的元素加一次) -->
    <!-- 实现思路:排序   去重    求和 -->
</body>
</html>
<script>
    function findTopSum(arr, k, m) {
    let newarr = [];
    arr.sort(function (a, b) {
      return a - b;
    })//此处的arr已重新排序  
    // return arr;  [1, 3, 3, 5, 9, 24]
    var firstarr = new Set(arr);//给新排序的数组去重 伪数组  
    // return firstarr;   Set(5) { 1, 3, 5, 9, 24 }
    var secondarr = Array.from(firstarr);//将伪数组转换为真实的数组
    // return secondarr;   [1, 3, 5, 9, 24]

    // console.log(secondarr[k - 1]);//  需要找到的第k大的值

    // console.log(secondarr[m - 1]);//  需要找到的第m大的值

    for (let i = 0; i < arr.length; i++) {
      if (arr[i] == secondarr[k - 1] || arr[i] == secondarr[m - 1]) {
        newarr.push(arr[i])
      }
    }
    // console.log(newarr);  //[3, 3,3,3, 5]

    let sum = newarr.reduce(function (newsum, item, index) {
      newsum += item;
      return newsum;
    })
    return sum;
  }

  console.log(findTopSum([1, 3, 24, 3, 5, 3, 9, 3], 2, 3)); //17
</script>